﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public static class String
    {
        /// <summary>
        /// 扩展类和扩展方法必须是静态的，扩展方法的第一个参数必须为原有类自身，this关键字是必须的
        /// </summary>

            public static string ToIsNULLString(this object self)
            {
                if (self == null)
                {
                    return "";
                }
                return self.ToString();
            }

        /// <summary>
        /// 超出长度的字符串以...结束
        /// </summary>
        /// <param name="self"></param>
        /// <param name="maxlength"></param>
        /// <returns></returns>
            public static string ToMaxLength(this object self,int maxlength)
            {
                if (self == null)
                {
                    return "";
                }
                if (self.ToString().Length > maxlength) { return self.ToString().Substring(0, maxlength)+"..."; }
                return self.ToString();
            }
    }
}
